/**
 * Created by Administrator on 2017/5/12.
 */

function checkUrl(url){
    var reg = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
    if(reg.test(url)){
        return true;
    }else{
        return false;
    }
}
$(document).on("click", "#sd4", function() {
    $.modal({
        title: "我的微网店链接",
        text: '<div class="set_shop_link weui_cell_bd weui_cell_primary">'+
        '<textarea id="shopUrl" class="weui_textarea" placeholder="填入网店链接,例：https://chaoshi.jd.com" rows="3"></textarea>'+
        '</div>',
        buttons: [
            { text: "取消", onClick: function(){
                return;
            } },
            { text: "完成", className: "default",onClick:function(){
                var shopUrl = $('#shopUrl').val();
                if(checkUrl(shopUrl)){
                    $.ajax({
                        url:API_URL+UPDATE_SHOPURL,
                        type:'post',
                        dataType:'jsonp',
                        data:{
                            userId:1,
                            shopUrl:shopUrl
                        },
                        success:function(data){
                            if(data.ok==true){
                                $.toast('修改成功!');
                            }
                        },
                        error:function(data){
                            $.alert(data.msg);
                        }
                    })
                }else{
                    $.toptips('网店链接不正确!','info');
                }
            }}
        ]
    });
});
/**
 * 获取分类
 */
function getShopType(){
    $.ajax({
        url: API_URL + GET_CATEGORYS_URL,
        type: 'get',
        dataType: 'jsonp',
        data:{type:4},
        success: function (data) {
            //分类拼接
            var datas= data.result;
            var json = {};
            var arrs = [];
            for(var i = 0;i<datas.length;i++){
                json = {title:datas[i].name,value:datas[i].id};
                arrs.push(json);
            }
            $("#shop_type").select({
                title: "选择来源",
                items: arrs,
                onChange: function(d) {
                    $('#type_from').val(d.values);
                    $.ajax({
                        url:API_URL+UPDATE_SHOPURL,
                        type:'post',
                        dataType:'jsonp',
                        data:{
                            userId:1,
                            shopType: d.values
                        },
                        success:function(data){
                            if(data.ok==true){
                                $.toptips('网店类型设置成功！','ok')
                            }
                        },
                        error:function(data){
                            $.alert(data.msg);
                        }
                    })
                }
            });
        }
    });
};
getShopType();
var userId =$.fn.cookie('userId');
$.ajax({
    url:API_URL+GET_USER_INFO,
    type:'post',
    dataType:'jsonp',
    data:{id:userId},
    success:function(data){
        console.log(data);
        if(data.ok==true){
            var datas = data.result;
            if(datas.isPhoneBind==1){
                $('#mobileverify').html('<span class="f-blue">已绑定</span>');
                $('#mobileverify').closest('.weui_cell').removeAttr('href');
                $('#mobileverify').closest('.weui_cell').on('click',function(){
                    $.modal({
                        title: "您已经通过手机认证，是否修改？",
                        buttons: [
                            { text: "确认", onClick: function(){
                                window.location.href='phoneCertification.html';
                            } },
                            { text: "取消", className: "default",onClick:function(){
                                $('#mobileverify').closest('.weui_cell').attr('href','phoneCertification.html');
                                window.location.href='set.html';
                            }}
                        ]
                    });
                })
            }
            if(datas.isPhoneBind==0){
                $('#mobileverify').html('<span class="f-blue">未绑定</span>');
            }
            if(datas.idVerifyStatus==1){
                $('#nameverify').html('<span class="f-blue">已认证</span>');
                $('#nameverify').closest('.weui_cell').addClass('noclick');
            }
            if(datas.idVerifyStatus==0){
                $('#nameverify').html('<span class="f-red">未认证</span>');
                $('#nameverify').closest('.weui_cell').removeClass('noclick');
            }
            if(datas.merchantStatus == 1){
                $('#shopverify').html('<span class="f-blue">审核中</span>');
                $('#shopverify').closest('.weui_cell').addClass('noclick');
            }
            if(datas.merchantStatus == 2){
                $('#shopverify').html('<span class="f-green">已通过</span>');
                $('#shopverify').closest('.weui_cell').addClass('noclick');
            }
            if(datas.merchantStatus == 0){
                $('#shopverify').html('<span class="f-red">已拒绝</span>');
            }
            if(datas.merchantStatus == -1){
                $('#shopverify').closest('.weui_cell').removeClass('noclick');
                $('#shopverify').html('<span class="f-red">未申请</span>');
            }
        }
    }
})
